package com.syq.service.impl;


import com.syq.bean.Menu;
import com.syq.bean.Role;
import com.syq.bean.Users;
import com.syq.dao.RoleDao;
import com.syq.dao.UsersDao;
import com.syq.dao.impl.RoleDaoImpl;
import com.syq.dao.impl.UsersDaoImpl;
import com.syq.service.UsersService;

import java.util.ArrayList;
import java.util.List;

public class UsersServiceImpl implements UsersService {
    private UsersDao usersDao = new UsersDaoImpl();
    private RoleDao roleDao = new RoleDaoImpl();
    @Override
    public Users login(String userName, String userPassword) {
        Users user = usersDao.login(userName,userPassword);
        if(user==null) {
            return null;
        }
        //根据id查询信息（三表）
        Integer roleId = user.getRoleId();
        Role role = roleDao.findAllById(roleId);
        //分级
        List<Menu> menuList = role.getMenuList();//未分级
        List<Menu> newMenuList = new ArrayList<>();//分级后
        for ( Menu m : menuList) {
            if(m.getUpmenuId()==0) {//1级菜单
                List<Menu> secondList = new ArrayList<>();
                for(Menu m2 : menuList) {
                    if(m2.getUpmenuId()==m.getMenuId()) {
                        secondList.add(m2);
                    }
                }
                m.setSecondMenuList(secondList);
                newMenuList.add(m);
            }

        }
        role.setMenuList(newMenuList);
        user.setRole(role);
        return user;
    }

    @Override
    public List<Users> getUsersList(int pageIndex, int pageSize) {
        return usersDao.getUsersList(pageIndex,pageSize);
    }

    @Override
    public int total() {
        return usersDao.total();
    }

    @Override
    public int insertUser(Users user) {
        return usersDao.insertUser(user);
    }

    @Override
    public int updateUser(Users user) {
        return usersDao.updateUser(user);
    }

    @Override
    public Users findById(int userId) {
        return usersDao.findById(userId);
    }

    @Override
    public int delUser(String uid) {
        return usersDao.delUser(uid);
    }
}
